<?php
/**
 * Created by PhpStorm.
 * User: t
 * Date: 2024/4/29
 * Time: 15:53
 */
$hostName = env('database.hostname', '127.0.0.1');
$database = env('database.database', '');
$userName = env('database.username', 'root');
$password = env('database.password', '');
$hostPort = env('database.hostport', '3306');
$prefix         = env('database.prefix', '{$prefix}');
$charset = 'utf8mb4';

$pdo = new PDO("mysql:host={$hostName};port={$hostPort}", $userName, $password, array(
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,

));
$pdo->query("USE `{$database}`");

//    ['name' => $prefix.'user_consumption', 'field' => 'open_id', 'type' =>'add' 'desc' =>" varchar(36) NOT NULL DEFAULT '' "],

//    ['name' => $prefix.'user_consumption', 'field' => 'id,key,', 'type' =>'insert'  'desc' =>" varchar(36) NOT NULL DEFAULT '' "],

$field = [

    ];

foreach ($field as $value){
    if ($value['type'] == 'add'){
        $stmt = $pdo->query("SHOW COLUMNS FROM `{$value['name']}` WHERE `Field` = '{$value['field']}'");
        if (!$stmt->fetch(PDO::FETCH_ASSOC)) {
            // 字段不存在，添加字段
            $sql = "ALTER TABLE `{$value['name']}`  ADD  `{$value['field']}` {$value['desc']} ";
            $pdo->exec($sql);
        }
    }elseif ($value['type'] == 'update'){
        $sql = "ALTER TABLE `{$value['name']}`  MODIFY COLUMN  `{$value['field']}` {$value['desc']} ";
    }elseif ($value['type'] == 'insert'){
        $sql = "INSERT INTO  `{$value['name']}` ({$value['field']}) values ({$value['desc']}) ";
    }
}




$conn = new mysqli($hostName, $userName, $password, $database);
foreach ($sql as $value){

    $conn->query($value);
}

// 关闭连接
$conn->close();
